package com.sfy.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sfy.auth.application.convert.AuthRoleDTOConverter;
import com.sfy.auth.application.dto.AuthRoleDTO;
import com.sfy.auth.common.entity.Result;
import com.sfy.auth.domain.entity.AuthRoleBO;
import com.sfy.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 新增角色
     *
     * @param authRoleDTO
     * @return
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController:add:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getRoleKey(), "角色标志不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleName(), "角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.AuthRoleDTOToAuthRoleBO(authRoleDTO);
            boolean add = authRoleDomainService.add(authRoleBO);
            return Result.OK(add);
        } catch (Exception e) {
            return Result.Fail();
        }
    }


    /**
     * 更新角色
     *
     * @param authRoleDTO
     * @return
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController:update:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleKey(), "角色标志不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleName(), "角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.AuthRoleDTOToAuthRoleBO(authRoleDTO);
            boolean update = authRoleDomainService.update(authRoleBO);
            return Result.OK(update);
        } catch (Exception e) {
            return Result.Fail();
        }
    }

    /**
     * 删除用户
     *
     * @param authRoleDTO
     * @return
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController:delete:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.AuthRoleDTOToAuthRoleBO(authRoleDTO);
            boolean delete = authRoleDomainService.delete(authRoleBO);
            return Result.OK(delete);
        } catch (Exception e) {
            return Result.Fail();
        }
    }
}
